package com.hnevc.jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import com.hnevc.jdbcdao.DBUtils;

public class TransactionDemo {

	public static void main(String[] args) {
		DBUtils db = new DBUtils();
		Connection conn = null;
		try {
			conn = db.getConnection();
			boolean autoCommit = conn.getAutoCommit();
			if(autoCommit ){
				conn.setAutoCommit(false);
			}
			Statement stmt = conn.createStatement();
			String sql1 = "INSERT users (id,name,password, email,birthday) "
					+ "values(100,'王五','123','wangwu@163.com','1989-9-9')";
			String sql2 = "INSERT users (id,name,password, email,birthday) "
					+ "values(101,'王五1','1231','wangwu1@163.com','1989-9-10')";
			String sql3 = "INSERT users (id,name,password, email,birthday) "
					+ "values(101,'王五2','1232','wangwu2@163.com','1989-9-11')";
			db.executeUpdate(sql1,null);
			db.executeUpdate(sql2,null);
			db.executeUpdate(sql3,null);
			conn.commit();
			conn.setAutoCommit(autoCommit);
		} catch (Exception e) {
			if(conn != null){
				try {
					conn.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			e.printStackTrace();
		}finally {
			try {
				db.closeAll();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}
